package com.fiberhome.boot.demo.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.fiberhome.boot.utils.RedisUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping(value="/redis")
@Api(value = "redis测试", description="redis测试", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class TestRedisContrller implements CommandLineRunner{

	@Autowired
	StringRedisTemplate stringRedisTemplate;
	
	@Autowired
	RedisTemplate<String, Object> objectRedisTemplate;
	
	RedisUtils util;
	
	@Override
	public void run(String... arg0) throws Exception {
		
		RedisUtils redisUtils = new RedisUtils(stringRedisTemplate,objectRedisTemplate);
		util = redisUtils;
	}
	
	@ApiOperation(value="根据key获取String", notes="根据key获取String")
	@RequestMapping(value="/stringValue",method=RequestMethod.GET)
	public String getStringValue(@RequestParam("key")String key){
		return util.getStringValue(key);
	}
	
	@ApiOperation(value="根据key获取Object", notes="根据key获取Object")
	@RequestMapping(value="/objectValue",method=RequestMethod.GET)
	public Object getObjectValue(@RequestParam("key")String key){
		return util.getObjectValue(key);
	}
	
	@ApiOperation(value="保存Object", notes="保存Object")
	@RequestMapping(value="/saveObject",method=RequestMethod.POST)
	public String  saveObject(String key,@RequestBody Object object){
		util.setObjectValue(key, object);
		return "saved";
	}
	
	@ApiOperation(value="保存String", notes="保存String")
	@RequestMapping(value="/saveString",method=RequestMethod.POST)
	public String  saveObject(String key,String value){
		util.setStringValue(key, value);
		return "saved";
	}
	
	
	@ApiOperation(value="根据key删除键值对", notes="根据key删除键值对")
	@RequestMapping(value="/delete",method=RequestMethod.GET)
	public String  delete(@RequestParam("key")String key){
		util.deleteStringValue(key);
		return "deleted";
	}

}
